﻿/*
 * Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2023-2023. All rights reserved.
 */

#ifndef PORTABLE_H
#define PORTABLE_H

#ifndef HAVE_CONFIG_H
#define PACKAGE "dfu-util"
#define PACKAGE_VERSION "0.10-msvc"
#define PACKAGE_STRING "dfu-util 0.10-msvc"
#define PACKAGE_BUGREPORT "http://sourceforge.net/p/dfu-util/tickets/"
#include <io.h>
using Offt = long int;
#endif /* HAVE_CONFIG_H */

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#define HAVE_WINDOWS_H
#ifdef HAVE_NANOSLEEP
#include <time.h>
#define milli_sleep(msec) do { \
    if (msec != 0) { \
    struct timespec nanosleepDelay = { (msec) / 1000, ((msec) % 1000) * 1000000 }; \
    nanosleep(&nanosleepDelay, NULL); \
  } } while (0)
#elif defined HAVE_WINDOWS_H
#define MILLI_SLEEP(msec) \
    do { \
        if ((msec) != 0) { \
            Sleep(msec); \
        } \
    } while (0)
#else
# error "Can't get no sleep! Please report"
#endif /* HAVE_NANOSLEEP */

#ifdef HAVE_ERR
#include <err.h>
#else
#include <cerrno>
#include <cstring>
#define PRINTFX(format, args...) do { \
    int buffersize = 4096; \
    char buffer[buffersize]; \
    if (memset_s(buffer, buffersize, 0, buffersize) == EOK) { \
        if (sprintf_s(buffer, buffersize, format, args) != -1) { \
            QString x = buffer; \
            SetEditErrorString(x); \
        } \
    }} while (0)
# define WARNX(format, args...) do { \
    int buffersize = 4096; \
    char buffer[buffersize]; \
    if (memset_s(buffer, buffersize, 0, buffersize) == EOK) { \
        if (sprintf_s(buffer, buffersize, format, args) != -1) { \
            QString x = buffer; \
            SetEditErrorString(x); \
        } \
    }} while (0)
# define ERRX(eval, format, args...) do { \
    int buffersize = 4096; \
    char buffer[buffersize]; \
    if (memset_s(buffer, buffersize, 0, buffersize) == EOK) { \
        if (sprintf_s(buffer, buffersize, format, args) != -1) { \
            QString x = buffer; \
            SetEditErrorString(x); \
        } \
    } \
    SetResult(eval); \
    if (isOneTimeBurn != 0) { \
        Global::CloseApp(result); \
    } \
    return eval;} while (0)
# define ERR(eval, format, args...) do { \
    int buffersize = 4096; \
    char buffer[buffersize]; \
    if (memset_s(buffer, buffersize, 0, buffersize) == EOK) { \
        if (sprintf_s(buffer, buffersize, format, args) != -1) { \
            QString x = buffer; \
            SetEditErrorString(x); \
        } \
    } \
    SetResult(eval); \
    return eval;} while (0)
#endif /* HAVE_ERR */

#ifdef HAVE_SYSEXITS_H
#include <sysexits.h>
#else
namespace PortableErro {
    const unsigned int EX_OK = 0; // successful termination
    const unsigned int EX_USAGE = 64; // command line usage error
    const unsigned int EX_DATAERR = 65;
    const unsigned int EX_NOINPUT = 66;
    const unsigned int EX_SOFTWARE = 70; // internal software error
    const unsigned int EX_CANTCREAT = 73; // input/output error
    const unsigned int EX_IOERR = 74; // input/output error
    const unsigned int EX_PROTOCOL = 76; // input/output error
}
#endif /* HAVE_SYSEXITS_H */

#ifndef O_BINARY
# define O_BINARY   0
#endif

#endif /* PORTABLE_H */
